package com.cjzuji.rental.dao.impl;

import com.cjzuji.rental.dao.OrderUserDao;
import com.cjzuji.rental.dao.XWuUserDao;
import com.cjzuji.rental.mapper.OrderUserMapper;
import com.cjzuji.rental.pojo.OrderUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class XWuUserDaoImpl implements XWuUserDao {

    private static final Logger logger = LoggerFactory.getLogger(XWuUserDao.class);
    @Autowired
    OrderUserMapper orderUserMapper;
    @Override
    @Transactional
    public void save( Long orderID,Map<String, Object> item,Map<String,Object> detail,String phone){
        Map<String, Object> userAddressDto = (Map<String, Object>) detail.get("orderAddressDto");
        Map<String, Object> userInfo = (Map<String, Object>) detail.get("userOrderInfoDto");
        //身份证上传
        String idCard = item.get("idCard") != null ? item.get("idCard").toString() : "";
        String idCardImg = userInfo.get("idCardFrontUrl") != null ? userInfo.get("idCardFrontUrl").toString() : "";
        String idCardBackImg = userInfo.get("idCardBackUrl") != null ? userInfo.get("idCardBackUrl").toString() : "";
        String faceImg = userInfo.get("faceImage") != null ? userInfo.get("faceImage").toString() : "";
        OrderUser orderUser = OrderUser.builder()
                .orderId(orderID)
                .userName(item.get("user_name").toString())
                .mobile(phone)
                .province(userAddressDto.get("provinceStr").toString())
                .city(userAddressDto.get("cityStr").toString())
                .county(userAddressDto.get("areaStr").toString())
                .address(userAddressDto.get("street").toString())
                .addressInfo(userAddressDto.get("provinceStr").toString()+userAddressDto.get("cityStr").toString()+userAddressDto.get("areaStr").toString()+userAddressDto.get("street").toString())
                .idCard(idCard)
                .idCardName(userInfo.get("realName").toString())
                .idCardImg(idCardImg)
                .idCardBlackImg(idCardBackImg)
                .faceImg(faceImg)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        try {
            orderUserMapper.insert(orderUser);
        }catch (Exception e){
            logger.info("订单用户添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }
}
